/**
 * 智能填写
 * @author Moxun<mxnstrive@gmail.com>
 * @copyright © 2024 http://jinpin.love/
 */
 
 import content from '@/utils/content'
 
 /**
  * 文字填写
  * @param {*} ctx 
  */
 function TextFiller(ctx) {
     let result = ''
     ctx.keyword.keys.some(k => {
         const words = content.pickWords(content.matchWords(ctx.content, k.keywords))
         if (words) {
             result = words
             return true
         }
     })
     return result
 }
  
 function MutiTextFiller(ctx) {
     let result = ''
     ctx.keyword.keys.forEach(k => {
         const words = content.pickWords(content.matchWords(ctx.content, k.keywords), 0)
         result += words
     })
     return result
 }
  
 function MapKeywordContentTextFiller(ctx) {
     let result = ''
     ctx.keyword.keys.forEach(k => {
         const keywords = content.matchKeywords(ctx.content, k.keywords)
         result += keywords.map(n => n.keywordContent).join(',')
     })
     return result
 }
  
 /**
  * 单选框填写
  * @param {*} ctx 
  */
 function RadioFiller(ctx) {
     let result = ''
     ctx.keyword.keys.some(k => {
         const words = content.pickWords(content.matchWords(ctx.content, k.keywords))
         if (words) {
             result = k.keyId
             return true
         }
     })
     return result
 }
  
 /**
  * 复选框填写
  * @param {*} ctx 
  */
 function CheckBoxFiller(ctx) {
     let result = []
     ctx.keyword.keys.forEach(k => {
         // 默认勾选
         if (k.defaultChecked) {
             result.push(k.keyId)
             return
         }
         const words = content.pickWords(content.matchWords(ctx.content, k.keywords))
         if (words) {
             result.push(k.keyId)
         }
     })
     return result
 }
  
 /**
  * 合并文字填写
  * @param {*} ctx 
  */
 function ConcatTextFiller(ctx) {
     let result = ''
     ctx.keyword.keys.forEach(k => {
         result = k.keywords.filter(n => n.isSelected && n.isPrivate).map(n => n.keyword).join(',');
     })
     return result
 }
  
 /**
  * 上传文件填写
  * @param {*} ctx 
  * @param {*} propName 
  */
 function UploadFileFiller(ctx, propName) {
     const urls = ctx.keyword.keys.map(k => {
         const keywords = content.matchKeywords(ctx.content, k.keywords)
         return keywords.flatMap(e => e.uploadFiles.split('|'))
     }).flatMap(e => e)
     ctx.target[propName] = urls
     return urls.map(u => ({ url: u }))
 }
  
 export {
     TextFiller,
     MutiTextFiller,
     MapKeywordContentTextFiller,
     RadioFiller,
     CheckBoxFiller,
     ConcatTextFiller,
     UploadFileFiller,
 }